<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>健康记录管理 - 宠物医院管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet">
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- 侧边栏 -->
            <div class="col-md-2 p-0">
                <%@ include file="../common/admin-sidebar.jsp" %>
            </div>

            <!-- 主内容区域 -->
            <div class="col-md-10">
                <div class="main-content p-4">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">健康记录管理</h1>
                    <div class="btn-toolbar mb-2 mb-md-0">
                        <div class="btn-group me-2">
                            <a href="${pageContext.request.contextPath}/health/add" class="btn btn-sm btn-outline-secondary">
                                <i class="fas fa-plus"></i> 添加记录
                            </a>
                        </div>
                    </div>
                </div>

                <!-- 消息提示 -->
                <c:if test="${not empty param.success}">
                    <div class="alert alert-success alert-dismissible fade show" role="alert">
                        ${param.success}
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                </c:if>
                <c:if test="${not empty param.error}">
                    <div class="alert alert-danger alert-dismissible fade show" role="alert">
                        ${param.error}
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                </c:if>

                <!-- 搜索表单 -->
                <div class="card mb-4">
                    <div class="card-header">
                        <h5 class="card-title mb-0">搜索条件</h5>
                    </div>
                    <div class="card-body">
                        <form method="get" action="${pageContext.request.contextPath}/health/search">
                            <div class="row">
                                <div class="col-md-3">
                                    <label for="petName" class="form-label">宠物姓名</label>
                                    <input type="text" class="form-control" id="petName" name="petName" 
                                           value="${petName}" placeholder="输入宠物姓名">
                                </div>
                                <div class="col-md-3">
                                    <label for="diagnosis" class="form-label">诊断结果</label>
                                    <input type="text" class="form-control" id="diagnosis" name="diagnosis" 
                                           value="${diagnosis}" placeholder="输入诊断关键词">
                                </div>
                                <div class="col-md-3">
                                    <label for="doctorName" class="form-label">医生姓名</label>
                                    <input type="text" class="form-control" id="doctorName" name="doctorName" 
                                           value="${doctorName}" placeholder="输入医生姓名">
                                </div>
                                <div class="col-md-3">
                                    <label for="visitDateStart" class="form-label">就诊开始日期</label>
                                    <input type="date" class="form-control" id="visitDateStart" name="visitDateStart" 
                                           value="${visitDateStart}">
                                </div>
                            </div>
                            <div class="row mt-3">
                                <div class="col-md-3">
                                    <label for="visitDateEnd" class="form-label">就诊结束日期</label>
                                    <input type="date" class="form-control" id="visitDateEnd" name="visitDateEnd" 
                                           value="${visitDateEnd}">
                                </div>
                                <div class="col-md-9 d-flex align-items-end">
                                    <button type="submit" class="btn btn-primary me-2">
                                        <i class="fas fa-search"></i> 搜索
                                    </button>
                                    <a href="${pageContext.request.contextPath}/health/list" class="btn btn-secondary">
                                        <i class="fas fa-undo"></i> 重置
                                    </a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>

                <!-- 健康记录列表 -->
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="card-title mb-0">健康记录列表</h5>
                        <div>
                            <button type="button" class="btn btn-sm btn-danger" onclick="batchDelete()" disabled id="batchDeleteBtn">
                                <i class="fas fa-trash"></i> 批量删除
                            </button>
                        </div>
                    </div>
                    <div class="card-body">
                        <c:choose>
                            <c:when test="${not empty pageResult.data}">
                                <form id="batchForm">
                                    <div class="table-responsive">
                                        <table class="table table-striped table-hover">
                                            <thead>
                                                <tr>
                                                    <th>
                                                        <input type="checkbox" id="selectAll" onchange="toggleSelectAll()">
                                                    </th>
                                                    <th>记录ID</th>
                                                    <th>宠物信息</th>
                                                    <th>就诊日期</th>
                                                    <th>症状</th>
                                                    <th>诊断结果</th>
                                                    <th>医生</th>
                                                    <th>费用</th>
                                                    <th>复诊日期</th>
                                                    <th>操作</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <c:forEach var="record" items="${pageResult.data}">
                                                    <tr>
                                                        <td>
                                                            <input type="checkbox" name="recordIds" value="${record.recordId}" 
                                                                   onchange="updateBatchDeleteButton()">
                                                        </td>
                                                        <td>${record.recordId}</td>
                                                        <td>
                                                            <div>
                                                                <strong>${record.pet.petName}</strong>
                                                                <br>
                                                                <small class="text-muted">${record.pet.petType} - ${record.pet.breed}</small>
                                                            </div>
                                                        </td>
                                                        <td>
                                                            <fmt:formatDate value="${record.visitDate}" pattern="yyyy-MM-dd"/>
                                                        </td>
                                                        <td>
                                                            <c:choose>
                                                                <c:when test="${fn:length(record.symptoms) > 30}">
                                                                    ${fn:substring(record.symptoms, 0, 30)}...
                                                                </c:when>
                                                                <c:otherwise>
                                                                    ${record.symptoms}
                                                                </c:otherwise>
                                                            </c:choose>
                                                        </td>
                                                        <td>
                                                            <c:choose>
                                                                <c:when test="${fn:length(record.diagnosis) > 30}">
                                                                    ${fn:substring(record.diagnosis, 0, 30)}...
                                                                </c:when>
                                                                <c:otherwise>
                                                                    ${record.diagnosis}
                                                                </c:otherwise>
                                                            </c:choose>
                                                        </td>
                                                        <td>${record.doctorName}</td>
                                                        <td>
                                                            <c:if test="${not empty record.cost}">
                                                                ¥<fmt:formatNumber value="${record.cost}" pattern="#,##0.00"/>
                                                            </c:if>
                                                        </td>
                                                        <td>
                                                            <c:if test="${not empty record.nextVisitDate}">
                                                                <fmt:formatDate value="${record.nextVisitDate}" pattern="yyyy-MM-dd"/>
                                                                <c:if test="${record.needsFollowUp()}">
                                                                    <span class="badge bg-warning">需复诊</span>
                                                                </c:if>
                                                            </c:if>
                                                        </td>
                                                        <td>
                                                            <div class="btn-group btn-group-sm">
                                                                <a href="${pageContext.request.contextPath}/health/view?id=${record.recordId}" 
                                                                   class="btn btn-outline-primary" title="查看详情">
                                                                    <i class="fas fa-eye"></i>
                                                                </a>
                                                                <a href="${pageContext.request.contextPath}/health/edit?id=${record.recordId}" 
                                                                   class="btn btn-outline-secondary" title="编辑">
                                                                    <i class="fas fa-edit"></i>
                                                                </a>
                                                                <button type="button" class="btn btn-outline-danger" 
                                                                        onclick="deleteRecord(${record.recordId})" title="删除">
                                                                    <i class="fas fa-trash"></i>
                                                                </button>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                </c:forEach>
                                            </tbody>
                                        </table>
                                    </div>
                                </form>

                                <!-- 分页 -->
                                <c:if test="${pageResult.totalPages > 1}">
                                    <nav aria-label="健康记录分页">
                                        <ul class="pagination justify-content-center">
                                            <c:if test="${pageResult.currentPage > 1}">
                                                <li class="page-item">
                                                    <a class="page-link" href="?page=${pageResult.currentPage - 1}&size=${pageSize}">上一页</a>
                                                </li>
                                            </c:if>
                                            
                                            <c:forEach begin="1" end="${pageResult.totalPages}" var="i">
                                                <c:choose>
                                                    <c:when test="${i == pageResult.currentPage}">
                                                        <li class="page-item active">
                                                            <span class="page-link">${i}</span>
                                                        </li>
                                                    </c:when>
                                                    <c:otherwise>
                                                        <li class="page-item">
                                                            <a class="page-link" href="?page=${i}&size=${pageSize}">${i}</a>
                                                        </li>
                                                    </c:otherwise>
                                                </c:choose>
                                            </c:forEach>
                                            
                                            <c:if test="${pageResult.currentPage < pageResult.totalPages}">
                                                <li class="page-item">
                                                    <a class="page-link" href="?page=${pageResult.currentPage + 1}&size=${pageSize}">下一页</a>
                                                </li>
                                            </c:if>
                                        </ul>
                                    </nav>
                                </c:if>

                                <!-- 分页信息 -->
                                <div class="d-flex justify-content-between align-items-center mt-3">
                                    <div>
                                        共 ${pageResult.totalCount} 条记录
                                    </div>
                                    <div>
                                        <select class="form-select form-select-sm" onchange="changePageSize(this.value)" style="width: auto;">
                                            <option value="10" ${pageSize == 10 ? 'selected' : ''}>每页10条</option>
                                            <option value="20" ${pageSize == 20 ? 'selected' : ''}>每页20条</option>
                                            <option value="50" ${pageSize == 50 ? 'selected' : ''}>每页50条</option>
                                        </select>
                                    </div>
                                </div>
                            </c:when>
                            <c:otherwise>
                                <div class="text-center py-4">
                                    <i class="fas fa-heartbeat fa-3x text-muted mb-3"></i>
                                    <p class="text-muted">暂无健康记录数据</p>
                                    <a href="${pageContext.request.contextPath}/health/add" class="btn btn-primary">
                                        <i class="fas fa-plus"></i> 添加第一条记录
                                    </a>
                                </div>
                            </c:otherwise>
                        </c:choose>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // 全选/取消全选
        function toggleSelectAll() {
            const selectAll = document.getElementById('selectAll');
            const checkboxes = document.querySelectorAll('input[name="recordIds"]');
            checkboxes.forEach(checkbox => {
                checkbox.checked = selectAll.checked;
            });
            updateBatchDeleteButton();
        }

        // 更新批量删除按钮状态
        function updateBatchDeleteButton() {
            const checkboxes = document.querySelectorAll('input[name="recordIds"]:checked');
            const batchDeleteBtn = document.getElementById('batchDeleteBtn');
            batchDeleteBtn.disabled = checkboxes.length === 0;
        }

        // 批量删除
        function batchDelete() {
            const checkboxes = document.querySelectorAll('input[name="recordIds"]:checked');
            if (checkboxes.length === 0) {
                alert('请选择要删除的记录');
                return;
            }

            if (confirm('确定要删除选中的 ' + checkboxes.length + ' 条记录吗？此操作不可恢复！')) {
                const form = document.getElementById('batchForm');
                form.action = '${pageContext.request.contextPath}/health/batchDelete';
                form.method = 'post';
                form.submit();
            }
        }

        // 删除单条记录
        function deleteRecord(recordId) {
            if (confirm('确定要删除这条健康记录吗？此操作不可恢复！')) {
                window.location.href = '${pageContext.request.contextPath}/health/delete?id=' + recordId;
            }
        }

        // 改变每页显示数量
        function changePageSize(size) {
            const url = new URL(window.location);
            url.searchParams.set('size', size);
            url.searchParams.set('page', '1');
            window.location.href = url.toString();
        }
    </script>
</body>
</html>
